package com.huangmb.anymark.app.api.mock;

import android.os.Handler;
import android.support.annotation.NonNull;
import com.alibaba.fastjson.JSON;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.geocoder.StreetNumber;
import com.huangmb.androidlib.utils.Logger;
import com.huangmb.anymark.app.api.BaseApi;
import com.huangmb.anymark.app.model.Position;
import com.huangmb.anymark.app.model.UserMarker;

import java.util.*;

/**
 * @author hmb
 * @version 1.0
 * @since 2016/4/21 15:45
 */
//TODO 编写模拟数据
public class UserMarkerApiMock {
    private static Handler handler = new Handler();


    public static void nearby(final LatLng from, final int range, String key, String sinceID, boolean simplify, @NonNull final BaseApi.Listener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<UserMarker> list = new ArrayList<>();
                Map<String, UserMarker> data = DataMock.getData();

                for (UserMarker item : data.values()) {
                    LatLng to = new LatLng(item.getLatitude(),item.getLongitude());// item.getPosition().toAmapLatLng();
                    float dis = AMapUtils.calculateLineDistance(from, to);
                    Logger.d("距离", from.toString() + " " + to.toString() + " " + dis + "米");
                    if (dis < range) {
                        list.add(item);
                    }
                }
                final String result = JSON.toJSONString(list);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        listener.onSuccess(result);
                    }
                });
            }
        }).start();
    }

    public static void get(@NonNull String id, boolean simplify, @NonNull BaseApi.Listener listener) {

    }

    public static void create(@NonNull final UserMarker item, final BaseApi.Listener listener) {
        String id = "" + (++DataMock.maxId);
        item.setId(id);
        DataMock.getData().put(id, item);
        if (listener != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    listener.onSuccess(JSON.toJSONString(item));
                }
            });
        }
    }

    public static void delete(String id, BaseApi.Listener listener) {

    }

    public static void modify(@NonNull UserMarker item, BaseApi.Listener listener) {

    }
}
